package com.yoyo.ad.yoyoplat;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.yoyo.ad.main.AdModelType;
import com.yoyo.ad.main.IAdClick;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.yoyoplat.sdk.YYNativeResponse;

import static com.yoyo.ad.confusion.AdManage.YOYO_PLAT;


class YPAd implements YoYoAd {

    private IAdClick mIAdClick;

    YPAd(YYNativeResponse yoYoAdBean, IAdClick adClick) {
        this.mYoYoAdBean = yoYoAdBean;
        this.mIAdClick = adClick;
    }

    private YYNativeResponse mYoYoAdBean;

    @Override
    public int getSource() {
        return YOYO_PLAT;
    }

    @Override
    public String getTitle() {
        if (mYoYoAdBean != null) {
            return mYoYoAdBean.getSubTitle();
        }
        return null;
    }

    @Override
    public String getDescription() {
        if (mYoYoAdBean != null) {
            return mYoYoAdBean.getTitle();
        }
        return null;
    }

    @Override
    public Bitmap getLogo() {
        return null;
    }

    @Override
    public String getIconUrl() {
        if (mYoYoAdBean != null) {
            return mYoYoAdBean.getIconUrl();
        }
        return null;
    }

    @Override
    public String getImgUrl1() {
        if (mYoYoAdBean != null) {
            return mYoYoAdBean.getImgList() != null ?
                    mYoYoAdBean.getImgList().get(0).getUrl() : null;
        }
        return null;
    }

    @Override
    public String getImgUrl2() {
        return null;
    }

    @Override
    public String getImgUrl3() {
        return null;
    }

    @Override
    public String getBrandName() {
        return "yoyo";
    }

    public int getModel() {
        return AdModelType.MODEL_IMAGE;
    }

    @Override
    public View getView() {
        return null;
    }

    @Override
    public boolean isNativeExpress() {
        return false;
    }

    @Override
    public void exposure(View view) {
        if (mYoYoAdBean != null && view != null) {
            mYoYoAdBean.onExposed(view);
            if (mIAdClick != null)
                mIAdClick.onAdShow();
        }
    }

    @Override
    public void onAdClicked(final View view, final ViewGroup viewGroup) {
        if (viewGroup != null)
            viewGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleClick(v);
                }
            });
        else
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleClick(v);
                }
            });
    }

    private void handleClick(View v) {
        if (mYoYoAdBean != null) {
            mYoYoAdBean.handleClick(v);
        }
        if (mIAdClick != null)
            mIAdClick.onAdClick(v);
    }

    @Override
    public void show(Activity activity) {

    }

    @Override
    public void release() {
    }
}
